import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_billi/http/core/hi_error.dart';
import 'package:flutter_billi/http/dao/favorite_dao.dart';
import 'package:flutter_billi/http/dao/video_detail_dao.dart';
import 'package:flutter_billi/model/video_detail.dart';
import 'package:flutter_billi/model/video_model.dart';
import 'package:flutter_billi/util/toast.dart';
import 'package:flutter_billi/util/view_util.dart';
import 'package:flutter_billi/widget/HiTab.dart';
import 'package:flutter_billi/widget/NavigationBar.dart';
import 'package:flutter_billi/widget/appBar.dart';
import 'package:flutter_billi/widget/expand_content.dart';
import 'package:flutter_billi/widget/video_header.dart';
import 'package:flutter_billi/widget/video_large_card.dart';
import 'package:flutter_billi/widget/video_tool_bar.dart';
import 'package:flutter_billi/widget/video_view.dart';

class VideoDetailPage extends StatefulWidget {
  final VideoModel videoModal;
  const VideoDetailPage(this.videoModal);

  @override
  _DetailPageState createState() => _DetailPageState();
}

class _DetailPageState extends State<VideoDetailPage> with TickerProviderStateMixin {
  TabController _controller;
  VideoDetailMo videoDetail;
  VideoModel videoModel;
  List<VideoModel> videoList = [];

  List tabs = ["简介", "评论"];

  @override
  void initState() {
    super.initState();
    _controller = TabController(length: tabs.length, vsync: this);
    // print("视屏详情页， vid: ${widget.videoModal.vid}");
    videoModel = widget.videoModal;
    _loadVideoDetail();
    // 设置黑色状态栏
    changeStatusBar(color: Colors.black, statusStyle: StatusStyle.LIGHT_CONTENT);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: MediaQuery.removePadding(
        removeTop: Platform.isIOS,
        context: context,
        child: videoModel.url != null
            ? Container(
                child: Column(
                  children: [
                    NavigationBar(
                      color: Colors.black,
                      statusStyle: StatusStyle.LIGHT_CONTENT,
                      height: Platform.isAndroid ? 0 : 46,
                    ),
                    _buildVideoView(),
                    _buildTabNavgation(),
                    Flexible(
                        child: TabBarView(
                      controller: _controller,
                      children: [
                        _buildDetailList(),
                        Container(
                          child: Text('敬请期待...'),
                        )
                      ],
                    ))
                  ],
                ),
              )
            : null,
      ),
    );
  }

  _buildVideoView() {
    var model = videoModel;
    return VideoView(
      model.url,
      cover: model.cover,
      overlayUI: VideoAppBar(),
    );
  }

  _buildTabNavgation() {
    // 使用Material 实现阴影大小
    return Material(
      elevation: 5,
      shadowColor: Colors.grey[100],
      child: Container(
        alignment: Alignment.centerLeft,
        padding: EdgeInsets.only(left: 20),
        height: 39,
        color: Colors.white,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _tabBar(),
            Padding(
              padding: EdgeInsets.only(right: 20),
              child: Icon(
                Icons.live_tv_rounded,
                color: Colors.grey,
              ),
            )
          ],
        ),
      ),
    );
  }

  _tabBar() {
    return HiTab(
      tabs.map((tab) => Tab(text: tab)).toList(),
      controller: _controller,
    );
  }

  _buildDetailList() {
    return ListView(
      padding: EdgeInsets.all(0),
      children: [
        ...buildContents(),
        ..._buildVideoList(),
      ],
    );
  }

  buildContents() {
    return [
      Container(
        child: VideoHeader(owner: this.videoModel.owner),
      ),
      ExpandedContent(mo: videoModel),
      VideoToolBar(
        detailMo: videoDetail,
        videoModel: videoModel,
        onLike: _onLike,
        onUnLike: _onUnLike,
        onFavorite: _onFavorite,
      ),
    ];
  }

  void _loadVideoDetail() async {
    try {
      VideoDetailMo result = await VideoDetailDao.get(videoModel.vid);
      print(result);
      setState(() {
        videoDetail = result;
        videoModel = result.videoInfo;
        videoList = result.videoList;
      });
    } on NeedAuth catch (e) {
      showWarnToast(e.message);
    } on NeedLogin catch (e) {
      showWarnToast(e.message);
    } catch (e) {
      showWarnToast(e.message);
    }
  }

  void _onUnLike() {}

  void _onLike() {}

  void _onFavorite() async {
    try {
      var result = await FavoriteDao.favorite(videoModel.vid, videoDetail.isFavorite);
      videoDetail.isFavorite = !videoDetail.isFavorite;
      if (videoDetail.isFavorite) {
        videoModel.favorite += 1;
      } else {
        videoModel.favorite -= 1;
      }
      setState(() {
        videoDetail = videoDetail;
        videoModel = videoModel;
      });
      showToast(result['msg']);
    } on NeedAuth catch (e) {
      showWarnToast(e.message);
      print(e);
    } on NeedLogin catch (e) {
      print(e);
    } on HiNetError catch (e) {
      print(e);
    }
  }

  _buildVideoList() {
    return videoList.map((VideoModel mo) => VideoLargeCard(videoModel: mo)).toList();
  }
}
